﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Billing.Windows
{
    /// <summary>
    /// Interaction logic for ExtraForCake.xaml
    /// </summary>
    public partial class ExtraForCake : Window
    {
        //BookCake m_CakeBooking;
        //public bool m_discountInRs = true;
        public double m_TotalAmount = 0;
        public double m_NetTotal = 0;
        public double m_ExtraAmount = 0;
        public bool m_ExtraInRs = false;

        public ExtraForCake(bool extraInRs)
        {
            InitializeComponent();

            try
            {
                //m_CakeBooking = bookCake;
                txtExtra.Focus();
                txtExtra.SelectAll();
                //m_discountInRs = discountInRs;
                lblTotalAfterDiscount.Content = m_TotalAmount;
                m_ExtraInRs = extraInRs;

                if (extraInRs)
                {
                    rDiscountHeaderLabel.Text = "Rs.";
                    lblDiscount.Content = "Extra in Rs.";
                }
                else
                {
                    rDiscountHeaderLabel.Text = "%";
                }
            }
            catch (Exception) { }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {

        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape || e.Key == Key.Enter)
            {
                if (lblInvalid.Visibility == Visibility.Visible && txtExtra.IsFocused)
                {
                    lblInvalid.Content = "Enter valid Discount or Clear the Discount";
                }
                //else
                //{
                //    txtDiscountReason.Focus();
                //}

                if (txtExtra.Text == string.Empty && lblInvalid.Visibility == Visibility.Collapsed)
                {
                    this.Close();
                }
                else if (lblInvalid.Visibility == Visibility.Collapsed)
                {
                    this.Close();
                }
            }
        }

        private void txtDiscount_TextChanged(object sender, TextChangedEventArgs e)
        {
            lblInvalid.Visibility = Visibility.Collapsed;

            if (txtExtra.Text.Trim() != string.Empty)
            {
                double value;

                if (double.TryParse(txtExtra.Text, out value))
                {
                    if (m_ExtraInRs)
                    {
                        m_ExtraAmount = value;
                        m_NetTotal = m_TotalAmount + m_ExtraAmount;
                    }
                    else
                    {
                        m_ExtraAmount = (m_TotalAmount * (value / 100));
                        m_NetTotal = m_TotalAmount + m_ExtraAmount;
                    }
                    lblTotalAfterDiscount.Content = m_NetTotal.ToString();
                }
                else
                {
                    lblInvalid.Visibility = Visibility.Visible;
                }
            }
            else
            {
                lblTotalAfterDiscount.Content = m_TotalAmount;
                //txtDiscountReason.Text = string.Empty;
            }
        }
    }
}
